package com.alliky.core.permission;

import android.Manifest;
import android.text.TextUtils;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;

public class RxPermissionUtils {

    private static final String TAG = RxPermissionUtils.class.getSimpleName();

    // 存储不再询问权限
    private static ArrayList<Permission> noAskPermission = new ArrayList<>();

    private static int requestCount;
    private static int refuseCount;

    /**
     * 申请多个权限，带拒绝权限并选择不再询问处理
     *
     * @param activity
     * @param permissions
     */
    public static void requestPermissions(FragmentActivity activity, RxPermissionListener listener, String... permissions) {
        noAskPermission.clear();
        requestCount = 0;
        refuseCount = 0;
        int length = permissions.length;
        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions
                .requestEach(permissions)
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(permission -> {
                            if (permission.granted) {
                                // 用户已经同意该权限
                                requestCount++;
                                handlePermission(activity, length, listener);
                            } else if (permission.shouldShowRequestPermissionRationale) {
                                // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                                requestCount++;
                                refuseCount++;
                                handlePermission(activity, length, listener);
                            } else {
                                // 用户拒绝了该权限，并且选中『不再询问』
                                requestCount++;
                                noAskPermission.add(permission);
                                handlePermission(activity, length, listener);
                            }
                        }
                );
    }

    /**
     * 处理多组权限问题
     *
     * @param activity
     * @param length
     * @param listener
     */
    private static void handlePermission(FragmentActivity activity, int length, RxPermissionListener listener) {
        //当权限个数等于申请次数时，证明所有权限都已申请完毕
        if (length == requestCount) {
            if (refuseCount == 0 && noAskPermission.size() == 0) {
                //权限全部申请通过
                listener.accept();
            } else if (noAskPermission.size() == 0) {
                //拒绝权限
                listener.refuse();
            } else {
                //拒绝权限，并勾选了不再提醒，弹窗提示去设置页
                listener.noAsk();
                String permissionName = getPermissionName();
            }
        }
    }

    /**
     * 获取权限的名字
     *
     * @return
     */
    private static String getPermissionName() {
        if (noAskPermission != null && noAskPermission.size() != 0) {
            StringBuilder sb = new StringBuilder();
            for (Permission permission : noAskPermission) {
                Log.e("lwd", permission.name);
                switch (permission.name) {
                    case Manifest.permission.CAMERA://相机权限
                        sb.append("相机");
                        sb.append("、");
                        break;
                    case Manifest.permission.WRITE_EXTERNAL_STORAGE://读写
                        sb.append("存储");
                        sb.append("、");
                        break;
                    case Manifest.permission.RECORD_AUDIO://录音
                        sb.append("麦克风");
                        sb.append("、");
                        break;
                    case Manifest.permission.REQUEST_INSTALL_PACKAGES://安装应用权限
                        sb.append("应用内安装其他应用");
                        sb.append("、");
                        break;
                    case Manifest.permission.CALL_PHONE://打电话
                        sb.append("电话");
                        sb.append("、");
                        break;
                }
            }
            return TextUtils.isEmpty(sb.toString()) ? "相关" : sb.toString().substring(0, sb.toString().length() - 1);
        }
        return "相关";
    }

}
